#!/bin/bash

##HACKABOT_HELP##
# Search google for someone's first name and see where they rank
# !googlepage Firstname Lastname
##HACKABOT_HELP##

MSG=`grep '^msg' | sed -e 's/^msg\s*//g'`
FIRST=`echo "$MSG" | awk '{print $1}'`
LAST=`echo "$MSG" | awk '{print $2}'`

if [ "$FIRST" == "" ] || [ "$LAST" == "" ]; then
	echo send "Usage: !googlepage Firstname Lastname"
	exit
fi

MAXRESULTS=1000

echo sendnext
echo Going to search google...
for i in `seq 0 10 $MAXRESULTS`
do 
    URL="http://www.google.com/search?q=$FIRST&start=$i"
    UA="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8b5) Gecko/20051022 Firefox/1.5"
    wget -q -U "$UA" "$URL" -O - | grep -q -i $LAST && echo -n "When $FIRST is googled, $LAST is found on page `echo "$i / 10 + 1" | bc`: $URL" && exit
    if [ $i -eq $MAXRESULTS ]
    then
        echo "Sorry, $LAST was not found in the first $MAXRESULTS results of the search $FIRST. Looks like you need to blog more." && exit
    fi
done
